<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // 使用最小体力爬楼梯
    var minCostClimbingStairs = function (cost) {
      // 1. dp数组的含义，代表“到达每一个台阶并继续往上爬所需要支付的体力”
      // 2. 递归公式: dp[i] = Math.min(dp[i - 1] + dp[i - 2]) + cost[i]
      let dp = []
      // 3. 初始化dp数组 4. 顺序，从前往后
      dp[0] = cost[0]
      dp[1] = cost[1]
      for (let i = 2; i <= cost.length - 1; i++) {
        dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i]
      }
      // 注意是到达倒数第一步或者倒数第二步的时候花费的体力，已经把向上爬要支付的算进去了
      return Math.min(dp[dp.length - 1], dp[dp.length - 2])
    };
    // 5. 推导[10, 15, 20]
    // [10, 15, 30] 取倒数两个的最小值
    console.log(minCostClimbingStairs([10, 15, 20]));
    console.log(minCostClimbingStairs([1, 100, 1, 1, 1, 100, 1, 1, 100, 1]));
  </script>
</body>

</html>